<template>
  <el-dialog
    title="新增部门"
    :visible="showDialog"
    width="600px"
    @close="onClose"
  >
    <el-form
      ref="deptForm"
      label-width="100px"
      size="mini"
      :model="deptForm"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <!-- 这里暂时不处理，因为要使用 el-select，需要列表数据 -->
        <el-select v-model="deptForm.managerId" placeholder="请选择">
          <el-option
            v-for="item in managers"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" />
      </el-form-item>
      <div style="text-align: center">
        <el-button type="primary" size="mini" @click="onOk">确认</el-button>
        <el-button size="mini" @click="onClose">取消</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>
<script>
import { getManagerList, getDepartments } from "@/api/department";
export default {
  name: "HeimahrAddDepat",

  data() {
    const createValidator = (key, message) => {
      return async (rule, value, callback) => {
        const res = await getDepartments();
        const item = res.find((item) => item[key] === value);
        item ? callback(new Error(message)) : callback();
      };
    };

    return {
      deptForm: {
        code: "",
        introduce: "",
        managerId: "",
        name: "",
        pid: "",
      },
      rules: {
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "部门编码长度 2 ~ 10个字符",
            trigger: "blur",
          },
          {
            validator: createValidator("code", "部门编码已存在"),
          },
        ],
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "部门名称长度 2 ~ 10个字符",
            trigger: "blur",
          },
          {
            validator: createValidator("name", "部门名称已存在"),
          },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "部门介绍长度 1 ~ 100个字符",
            trigger: "blur",
          },
        ],
        managerId: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
      },
      managers: [],
    };
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    pid: {
      type: Number,
      default: 0,
    },
  },
  mounted() {},
  created() {
    this.getManagers();
  },
  methods: {
    onClose() {
      this.$emit("update:showDialog", false);
    },
    onOk() {
      // 1. 先进行表单的手动校验
      this.$refs.deptForm.validate((isOk) => {
        console.log(isOk);
        if (!isOk) return;
        // todo: 如果校验成功，就执行添加新部门的功能：调接口
      });
    },
    async getManagers() {
      this.managers = await getManagerList();
      console.log(this.managers);
    },
    myValidator() {},
  },
};
</script>

<style lang="scss" scoped>
</style>